<?php
/**
 * 分类数组类
 * @author paco 2017-08-05
 */
namespace arrayset;

class Arrayset
{

	/**
	 * 把数组的健名设为主键值
	 * @param array $data 菜单数组
	 * @param string $key 主键名
	 * @return array
	 */
	public function setArrayKey($data, $key = "id")
	{
	    $arr = [];
	    if ($data) {
	        foreach ($data as $k => $v) {
	            $arr[$v['id']] = $v;
	        }
	    }
	    return $arr;
	}

	/**
	 * @todo 把数据集按排序组成树
	 * @author paco
	 * @param $data 引用型参数
	 * @param $pid 父ID
	 * @param $count 树深度
	 * @return array
	 */
	public function getTree(&$data, $pid = 0, $path = 0,$level=0,$pid_key="pid",$key="id")
	{
	    if (!isset($data['odl'])) {
	        $data = array('new' => [], 'odl' => $data);
	    }
	    foreach ($data['odl'] as $k => $v) {
	        if ($v[$pid_key] == $pid) {
	            $v['path']     = $path;
                $v['level']     = $level;
	            $data['new'][] = $v;
	            unset($data['odl'][$k]);

	            $this->getTree($data, $v[$key], $path . '-' . $v[$key],$level+1);
	        }
	    }
	    return $data['new'];
	}

	/**
	 * 把数组拼装成多维数组
	 * @param  [array] $data [key值 为ID的菜单列表]
	 * @return [array]       [二维或多维数组]
	 */
	public function getDeepTree($data, $pid_key = "pid", $key = "id")
	{
	    $tree = [];
	    if ($data) {
	        foreach ($data as $item) {
	            if (isset($data[$item[$pid_key]])) {
	                $data[$item[$pid_key]]['sub'][] = &$data[$item[$key]];
	            } else {
	                $tree[] = &$data[$item[$key]];
	            }
	        }
	    }
	    return $tree;
	}

	/**
     * @todo 判断ID是否有子类
     * @author paco jqcool.net
     * @param $id
     * @return boolearn
     */
    public function haveChild($id,$data,$pid_key ="pid")
    {
        $flag    = false;
        foreach ($data as $val) {
            if ($val[$pid_key] == $id) {
                $flag = true;
                break;
            }
        }
        return $flag;
    }


    /**
     * 获取某分类的直接子分类记录
     * @param  array  $categorys  数据集
     * @param  integer $catId     分类ID
     * @return array             返回该分类的直接子类数组
     */
    public function getSons($categorys, $catId = 0, $level = 0,$pid_key="pid",$key="id")
    {
        $sons = [];
        foreach ($categorys as $k => $item) {
            if ($item[$pid_key] == $catId) {
                $item['level'] = $level;
                $sons[]        = $item;
                unset($categorys[$k]); //注销当前节点数据，降低已没用的遍历
                $this->getSons($categorys, $item[$key], $level++);
            }
        }
        return $sons;
    }

    /**
     * 获取某个分类的所有子分类ID
     * @param  array  $categorys 分类数据集
     * @param  integer $catId     分类ID
     * @param  integer $self     是否包含传时来的ID
     * @return array             返回该分类下所有子类ID数组
     */
    public function getSonIds($categorys, $catId = 0, $self = 1,$pid_key="pid",$key="id")
    {
        $ids = [];
        if ($self) {$ids[] = $catId;}
        foreach ($categorys as $k => $item) {
            if ($item[$pid_key] == $catId) {
                $ids[] = $item[$key];
                unset($categorys[$k]); //注销当前节点数据，降低已没用的遍历
                $this->getSonIds($categorys, $item[$key], 0);
            }
        }
        return $ids;
    }


    public function getParentsIds($categorys,$catId = 0, $pid_key="pid",$key="id")
    {
        $ids =[];
        if(isset($categorys[$catId])){
            $path = $categorys[$catId]['path'];
            $ids = explode("-",$path);
            unset($ids[0]);
        }
        return $ids;
    }
}
